$(document).ready(function() {
    $("#selectall").click(function() {
        if ($("#selectall").prop("checked") == true) {
            $(".chek_all").prop("checked", true);
        } else {
            $(".chek_all").prop("checked", false);
        }
    });


    $("#add_client").click(function() {
        var chkArray = [];
        $(".chek_all:checked").each(function() {
            chkArray.push($(this).val());
        });
        var selected;
        selected = chkArray.join(',');
        $("#ac_property_id").val(selected);
        $("#myModal_client").modal('show');

    });
	
	
	$("#prp_id").select2(
            {
                placeholder: "Select Ref.No"
            });

	
	
    $("#add_prp_to_client").select2(
            {
                placeholder: "Select client"
            });


    $("#add_btn_client").click(function() {
	//alert('in');

        var ac_prp_id = $("#ac_property_id").val();
        var ac_member_id = $("#add_prp_to_client").val();

        $("#error_add_client").hide();
        if (ac_prp_id == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please select atleast one property');
            return false;
        }else if (ac_member_id == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please select client');
            $("#add_prp_to_client").css('border-color', 'red');
            return false;
        } else {
            var postdata = {};
            var obj = $(this);
            postdata['ac_member_id'] = ac_member_id;
            postdata['ac_prp_id'] = ac_prp_id;
            //alert(postdata);
            jQuery.post(BASE_PATH + '/property/addtoclient', postdata, function(result) {
                if (result == 1) {
                    $("#success_add_client").show();
                    window.setTimeout(function() {
                         $("#add_prp_to_client").select2("val","");
                         $("#ac_property_id").val('');
                         $(".chek_all").prop("checked", false);
                         $("#success_add_client").hide();
                         $("#myModal_client").modal('hide');
                    }, 3000);
                }
            });
        }


    });




	 $("#city_title").select2(
            {
                placeholder: "Select City"
            });
			
	 $("#area_title").select2(
            {
                placeholder: "Select Area"
            });
	

    $("#prp_address").select2(
            {
                placeholder: "Select Address"
            });
    $("#member_id").select2({
        placeholder: "Select Member"
    });

    $('.property-select').change(function() {

        var postdata = {};
        var property_type = $(this).val();
        if (property_type === '') {
        } else {
            postdata['property_type'] = property_type;
            jQuery.post(BASE_PATH + '/property/getpropertysublist', postdata, function(result) {
                jQuery('.property-subtype-select').html(result);
            });
        }

    });

    $('.property-city').change(function() {
        var postdata = {};
        var city_id = $(this).val();
        if (city_id === '') {
        } else {
            postdata['city_id'] = city_id;
            jQuery.post(BASE_PATH + '/property/getarealist', postdata, function(result) {
                $('.prp_area').html(result);
            });
        }

    });
	
		/*$("#send_email").click(function() {
			var chkArray = [];
			$(".chek_all:checked").each(function() {
				chkArray.push($(this).val());
			});
			var selected;
			selected = chkArray.join(',');
			$("#ac_property_id").val(selected);
			$("#myModal_property").modal('show');

		});*/
		
		
		
		$(".email_frnd").click(function(){
			//alert('in');
		var imag_src = $(this).attr('image_src');
        var status = $(this).attr('status');
        var property_price = $(this).attr('property_price');
        var property_name = $(this).attr('property_name');
        var property_id = $(this).attr('property_id');
        var ref_name = $(this).attr('ref_no') + property_id;
        var property_type = $(this).attr('property_type');
        $("#unique_all").val(property_type);
        if (property_type) {
            $("#col-lg-12").hide();

            var chkArray = [];
            $(".chek_all:checked").each(function() {
                chkArray.push($(this).val());
            });
            var selected;
            selected = chkArray.join(',');
            $("#property_all_id").val(selected);

        }
		
		
        $("#show_price").html(property_price);
        $("#status").html(status);
        $("#property_name").html(property_name);
        $("#prp_image").attr('src', imag_src);

        $("#ref_name").html(ref_name);
        $("#send_frnd_email").attr('property_id', property_id);

        if(property_type != 'all'){
            $("#myModal_property").modal('show');
        }else{
            if(chkArray.length != 0){
				//alert(chkArray.length);
                $("#myModal_property").modal('show');
				$("#send_email_error").hide();
            }else{
                $("#send_email_error").show();
            }
            
        }
        
    });

    $("#send_frnd_email").click(function() {
		//alert('in');
        var frnd_name = $("#frnd_name").val();
        var frnd_email = $("#frnd_email").val();
        var your_name = $("#your_name").val();
        var your_email = $("#your_email").val();
        var frnd_tel_no = $("#frnd_tel_no").val();
        var textarea_note = $("#textarea_note").val();
        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var property_id = $(this).attr('property_id');
		
		var unique_all = $("#unique_all").val();
		var property_all_id = $("#property_all_id").val();
		

        $("#error_frnd_name").hide();
        $("#error_frnd_email").hide();
        $("#error_valid_email").hide();
        $("#error_your_name").hide();
        $("#error_your_email").hide();

        $("#frnd_name").removeAttr('style');
        $("#frnd_email").removeAttr('style');
        $("#your_name").removeAttr('style');
        $("#your_email").removeAttr('style');


        if (frnd_name == '') {
            $("#error_frnd_name").show();
            $("#frnd_name").css('border-color', 'red');
            return false;
        } else if (frnd_email == '') {
            $("#error_frnd_email").show();
            $("#frnd_email").css('border-color', 'red');
            return false;
        } else if (frnd_email != '' && regex.test(frnd_email) == false) {
            $("#error_valid_email").show();
            $("#frnd_email").css('border-color', 'red');
            return false;
        } else if (your_name == '') {
            $("#error_your_name").show();
            $("#your_name").css('border-color', 'red');
            return false;
        } else if (your_email == '') {
            $("#error_your_email").show();
            $("#your_email").css('border-color', 'red');
            return false;
        } else if (your_email != '' && regex.test(your_email) == false) {
            $("#error_valid_email").show();
            $("#your_email").css('border-color', 'red');
            return false;
        }

        var postdata = {};
        var obj = $(this);
        postdata['frnd_name'] = frnd_name;
        postdata['frnd_email'] = frnd_email;
        postdata['your_name'] = your_name;
        postdata['your_email'] = your_email;
        postdata['frnd_tel_no'] = frnd_tel_no;
        postdata['textarea_note'] = textarea_note;
        postdata['property_id'] = property_id;
		postdata['unique_all'] = unique_all;
		postdata['property_all_id'] = property_all_id;
		//alert(postdata);
        jQuery.post(BASE_PATH + '/property/sendfriendmail', postdata, function(result) {
            $("#success_frnd_email").show();
            $("#success_frnd_email").html(result);
           /*window.setTimeout(function() {
                location.href = SITE_URL + "member";
            }, 3000);*/
        });

    });

	
});
	

	